<?php //pack11class

class Translator extends Module {

    public $menuItems=array("Показать все", "Лейблы", "Значения", "Свойства", "Поиск: <form action=\"{link}\" method=\"POST\"><input type=\"text\" name=\"search\" id=\"search\"/><input type=\"submit\" value=\"Искать\"/></form>");
    public $menuData=array();
    public $menu="";
    public $languages="";

    public function setup() {
        $setting=SettingsManager::getSetting("translate");
        $translateSetting=clone $setting;
        $queryObject=new GetQueryObject();
        $queryObject=GetQuery::useDefinedVars();

        foreach($this->menuItems as $key=>$value) {

            $translateSetting->setValue($key);
            $queryObject->setValue($translateSetting, GetQueryObject::SETTING);
            $query=GetQuery::generate($queryObject);

            if($key<4) {
                array_push($this->menuData, str_replace('\"','"',"<a href=\"?state=$query\">".$value."</a>"));
            } else {
                $form=str_replace('\"','"',$value);
                array_push($this->menuData, str_replace("{link}","?state=$query",$form));
            }

        }

        $this->menu=implode("&nbsp;|&nbsp;",$this->menuData);

        $langsData=array();
        $langsArray=array();
        foreach(LanguagesManager::getLanguages() as $language) {
            array_push($langsData,"`lang".$language->id."`");
            array_push($langsArray,$language->id);
        }
        $langs=implode(", ",$langsData);
        $this->languages=implode(",",$langsArray);
        
        $label_query="Select 'label' as `type`, `id`, $langs from ".MySQL::TABLE_LABELS;
        $value_query="Select 'value' as `type`, `id`, $langs from ".MySQL::TABLE_VALUES;
        $property_query="Select 'property' as `type`, `id`, $langs from ".MySQL::TABLE_PROPERTIES;

        $view=SettingsManager::getSetting("translate");
        $items=array();
        if(!$view->value || $view->value==0) {
            $items=MySQLGet::process("($label_query) UNION ($value_query) UNION ($property_query);");
        } else if($view->value==1) {
            $items=MySQLGet::process($label_query);
        } else if($view->value==2) {
            $items=MySQLGet::process($value_query);
        } else if($view->value==3) {
            $items=MySQLGet::process($property_query);
        } else {
            if(array_key_exists("search",$_POST)) {
                $s=$_POST["search"];
                $langsData=array();
                foreach(LanguagesManager::getLanguages() as $language) array_push($langsData,"`lang".$language->id."` like '%".$s."%'");
                $wherelangs=implode(" OR ",$langsData);
                $items=MySQLGet::process("($label_query where $wherelangs) UNION ($value_query where $wherelangs) UNION ($property_query where $wherelangs);");
            }
            
        }

        if(!$items) return false;

        foreach($items as $item) {
            $line=new TranslatorItem();
            $line->type=$item["type"];
            $line->id="ti_".$item["type"]."_".$item["id"];
            $line->_id=$item["id"];
            $line->setClass("TranslatorItem");
            foreach(LanguagesManager::getLanguages() as $language) {
                $input=new TextInput();
                $input->id="ti_".$item["type"]."_".$item["id"]."_".$language->id;
                $input->setClass("TranslatorInput");
                if(!is_null($item["lang".$language->id]))$input->extra_value=$item["lang".$language->id];
                $input->setBody($item["lang".$language->id]);
                $input->label=$language->label;
                $input->events='onkeydown="Translator.setState(this)" onkeyup="Translator.setState(this)" onblur="Translator.setState(this)" onclick="Translator.setState(this)"';
                $line->addChild($input);
            }
            $this->addChild($line);
        }
        $spacer=new Spacer();
        $spacer->addStyle("clear","both");
        $this->addChild($spacer);
    }

}
?>
